package com.huawei.hwid.api.common.apkimpl;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.Window;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.LoginHandler;
import com.huawei.hwid.api.common.o;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.c;
import com.huawei.hwid.core.d.l;
import com.huawei.hwid.core.d.m;
import com.huawei.hwid.core.d.n;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.secure.android.common.intent.SafeBundle;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.io.IOException;

public class DummyActivity extends Activity {
    private AccountManager a = null;
    private String b = "";
    private String c = "";
    private String d = "";
    private String e = "";
    private String f = "";
    private String g = "";
    private boolean h = false;
    private boolean i = false;
    private Intent j = null;
    private Bundle k = null;
    private Bundle l = null;
    private boolean m;

    class a implements AccountManagerCallback<Bundle> {
        final /* synthetic */ DummyActivity a;

        public a(DummyActivity dummyActivity) {
            this.a = dummyActivity;
        }

        public void run(AccountManagerFuture<Bundle> accountManagerFuture) {
            Bundle bundle;
            int i;
            String str;
            String str2 = "";
            e.b("AuthTokenCallBack", "AuthTokenCallBack::run==>", true);
            if (accountManagerFuture != null) {
                try {
                    bundle = (Bundle) accountManagerFuture.getResult();
                } catch (OperationCanceledException e) {
                    i = 3002;
                    e.d("AuthTokenCallBack", "AuthTokenCallBack OperationCanceledException:", true);
                    bundle = null;
                    str = "getAuthTokenByFeatures : OperationCanceledException occur";
                } catch (AuthenticatorException e2) {
                    i = 3003;
                    e.d("AuthTokenCallBack", "AuthTokenCallBack AuthenticatorException:", true);
                    bundle = null;
                    str = "getAuthTokenByFeatures : AuthenticatorException occur";
                } catch (IOException e3) {
                    i = 3004;
                    e.d("AuthTokenCallBack", "AuthTokenCallBack IOException:", true);
                    bundle = null;
                    str = "getAuthTokenByFeatures : IOException occur";
                }
            } else {
                bundle = null;
            }
            Object obj = str2;
            i = 0;
            if ((i == 0 || TextUtils.isEmpty(obj)) && bundle != null) {
                SafeBundle safeBundle = new SafeBundle(bundle);
                this.a.d = (String) safeBundle.get("authAccount");
                this.a.e = (String) safeBundle.get("accountType");
                this.a.c = (String) safeBundle.get("authtoken");
                this.a.a(this.a.c, this.a.d, i, safeBundle);
                return;
            }
            Parcelable errorStatus;
            if (bundle == null) {
                e.b("AuthTokenCallBack", "AuthTokenCallBack:run bundle is null", true);
                errorStatus = new ErrorStatus(i, "bundle is null");
            } else {
                e.b("AuthTokenCallBack", "AuthTokenCallBack:error", true);
                errorStatus = new ErrorStatus(i, obj);
            }
            Intent intent = new Intent();
            intent.setPackage(this.a.getPackageName());
            intent.putExtra(HwAccountConstants.EXTRA_IS_USE_SDK, false);
            intent.putExtra(HwAccountConstants.EXTRA_PARCE, errorStatus);
            c.b(this.a, intent);
            this.a.finish();
        }
    }

    class b implements AccountManagerCallback<Bundle> {
        final /* synthetic */ DummyActivity a;
        private boolean b;
        private String c;

        b(DummyActivity dummyActivity, String str, boolean z) {
            this.a = dummyActivity;
            this.c = str;
            this.b = z;
        }

        public void run(AccountManagerFuture<Bundle> accountManagerFuture) {
            if (accountManagerFuture != null) {
                try {
                    this.a.l = (Bundle) accountManagerFuture.getResult();
                    this.a.b(this.a.l);
                    this.a.finish();
                } catch (OperationCanceledException e) {
                    e.d("DummyActivity", "OperationCanceledException / ", true);
                    a("OperationCanceledException");
                    return;
                } catch (AuthenticatorException e2) {
                    e.d("DummyActivity", "AuthenticatorException / ", true);
                    a("AuthenticatorException");
                    return;
                } catch (IOException e3) {
                    e.d("DummyActivity", "IOException / ", true);
                    a("IOException");
                    return;
                } finally {
                    e.d("DummyActivity", "finally", true);
                }
            }
            e.d("DummyActivity", "finally", true);
        }

        private void a(String str) {
            if (this.b) {
                this.a.a(this.c, false, 1000);
            } else {
                this.a.a(53, str);
            }
        }
    }

    public static void a(Activity activity) {
        try {
            activity.getWindow().getAttributes().getClass().getDeclaredField("layoutInDisplayCutoutMode").set(activity.getWindow().getAttributes(), Integer.valueOf(1));
        } catch (RuntimeException e) {
            e.d("DummyActivity", "RuntimeException: " + e.getClass().getSimpleName(), true);
        } catch (Exception e2) {
            e.d("DummyActivity", "Exception: " + e2.getClass().getSimpleName(), true);
        }
    }

    protected void onCreate(Bundle bundle) {
        e.b("DummyActivity", "onCreate", true);
        super.onCreate(bundle);
        a((Activity) this);
        this.j = getIntent();
        if (this.j == null) {
            e.b("DummyActivity", "we got a wrong intent", true);
            finish();
            return;
        }
        a();
        if (!(this.j instanceof SafeIntent)) {
            this.j = new SafeIntent(this.j);
            setIntent(this.j);
        }
        this.k = this.j.getBundleExtra(HwAccountConstants.EXTRA_BUNDLE);
        if (this.k == null) {
            this.k = new Bundle();
        }
        this.m = this.k.getBoolean(CloudAccountManager.KEY_IS_TRANS_NAVIGATION_BAR, false);
        this.a = AccountManager.get(this);
        this.b = this.j.getStringExtra("requestTokenType");
        if (TextUtils.isEmpty(this.b)) {
            e.b("DummyActivity", "params invalid: tokenType is null", true);
            finish();
            return;
        }
        LoginHandler a = o.a();
        if (a == null) {
            e.b("DummyActivity", "params invalid: loginHandler is null", true);
            finish();
        } else if (this.k.getBoolean("IS_LOGIN_BY_ACTIVITY", false)) {
            d();
        } else if (com.huawei.hwid.core.d.b.b(this, "com.huawei.hwid.GET_AUTH_TOKEN")) {
            com.huawei.hwid.api.common.e.a((Context) this, a, null);
            this.k.putString("ServiceType", this.b);
            c();
        } else {
            b();
        }
    }

    private void a() {
        requestWindowFeature(1);
        Window window = getWindow();
        window.setFlags(HwAccountConstants.FLAG_TRANSLUCENT_STATUS, HwAccountConstants.FLAG_TRANSLUCENT_STATUS);
        if (VERSION.SDK_INT >= 19 && this.m) {
            window.setFlags(HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR);
        }
        if (com.huawei.hwid.core.d.b.a((Activity) this, Boolean.valueOf(true))) {
            com.huawei.hwid.core.d.b.a((Activity) this, true);
        }
    }

    private void b() {
        Account[] accountsByType = this.a.getAccountsByType("com.huawei.hwid");
        String[] strArr = new String[]{""};
        if (accountsByType == null || accountsByType.length <= 0) {
            this.a.getAuthTokenByFeatures("com.huawei.hwid", "com.huawei.hwid", strArr, this, this.k, this.k, new a(this), null);
            return;
        }
        this.k.putBoolean("chooseAccount", true);
        this.a.getAuthToken(accountsByType[0], getPackageName(), this.k, this, new a(this), null);
    }

    private void c() {
        this.j = new Intent("com.huawei.hwid.GET_AUTH_TOKEN");
        this.j.putExtras(this.k);
        this.j.putExtra(CloudAccountManager.KEY_IS_TRANS_NAVIGATION_BAR, this.m);
        this.j.setPackage("com.huawei.hwid");
        try {
            startActivityForResult(this.j, 1);
        } catch (Exception e) {
            e();
        }
    }

    private void d() {
        Intent intent = new Intent(HwAccountConstants.ACTION_HWID_LOGIN_BY_ACTIVITY);
        intent.setPackage("com.huawei.hwid");
        intent.putExtra(CloudAccountManager.KEY_IS_TRANS_NAVIGATION_BAR, this.m);
        try {
            startActivityForResult(intent, 2);
        } catch (Exception e) {
            e();
        }
    }

    private void e() {
        e.d("DummyActivity", "SDK can not start intent for GETTOKEN", true);
        Parcelable errorStatus = new ErrorStatus(15, "Access is not allowed");
        Intent intent = new Intent();
        intent.setPackage(getPackageName());
        intent.putExtra(HwAccountConstants.EXTRA_IS_USE_SDK, false);
        intent.putExtra(HwAccountConstants.EXTRA_PARCE, errorStatus);
        c.b(this, intent);
        finish();
    }

    private void a(int i, int i2, Intent intent) {
        Bundle bundle;
        int i3;
        String str = "";
        e.b("DummyActivity", "onActivityResult::resultCode ==> " + i2, true);
        if (-1 == i2 && intent != null) {
            Bundle extras = intent.getExtras();
            ErrorStatus a = a(extras);
            int errorCode = a.getErrorCode();
            String errorReason = a.getErrorReason();
            bundle = extras;
            i3 = errorCode;
            Object obj = errorReason;
        } else if (i2 == 0) {
            Parcelable errorStatus = new ErrorStatus(3002, "getAuthTokenByFeatures : OperationCanceledException occur");
            Intent intent2 = new Intent();
            intent2.setPackage(getPackageName());
            intent2.putExtra(HwAccountConstants.EXTRA_IS_USE_SDK, false);
            intent2.putExtra(HwAccountConstants.EXTRA_PARCE, errorStatus);
            c.b(this, intent2);
            finish();
            return;
        } else {
            e.b("DummyActivity", "OperationCanceledException", true);
            bundle = null;
            i3 = 3002;
            String str2 = "getAuthTokenByFeatures : OperationCanceledException occur";
        }
        if ((i3 == 0 || TextUtils.isEmpty(obj)) && bundle != null) {
            a(this.c, this.d, i3, new SafeBundle(bundle));
            return;
        }
        if (bundle == null) {
            e.b("DummyActivity", "AuthTokenCallBack:run bundle is null", true);
            errorStatus = new ErrorStatus(i3, "bundle is null");
        } else {
            e.b("DummyActivity", "AuthTokenCallBack:error", true);
            errorStatus = new ErrorStatus(i3, obj);
        }
        intent2 = new Intent();
        intent2.setPackage(getPackageName());
        intent2.putExtra(HwAccountConstants.EXTRA_IS_USE_SDK, false);
        intent2.putExtra(HwAccountConstants.EXTRA_PARCE, errorStatus);
        c.b(this, intent2);
        finish();
    }

    private void b(int i, int i2, Intent intent) {
        e.b("DummyActivity", "loginResult#ok", true);
        HwAccount a = new HwAccount().a(intent.getExtras());
        String str = "";
        if (!TextUtils.isEmpty(a.b())) {
            str = a.b();
        }
        Object q = a.q();
        if (TextUtils.isEmpty(q) || HwAccountConstants.NULL.equalsIgnoreCase(q)) {
            String a2 = n.a(this, 0);
            if (a2 == null) {
                a2 = "";
            }
            a.p(a2);
        }
        com.huawei.hwid.d.a.a((Context) this).a(a);
        CloudAccount[] a3 = com.huawei.hwid.api.common.e.a((Context) this);
        o.a().onLogin(a3, com.huawei.hwid.api.common.e.a(a3, str));
        com.huawei.hwid.c.a.a(this).a((Context) this, a);
        finish();
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        e.b("DummyActivity", "onActivityResult::requestCode==>", true);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.d("DummyActivity", "InterruptedException", true);
        }
        if (1 == i) {
            a(i, i2, intent);
        } else if (2 != i) {
        } else {
            if (i2 != -1 || intent == null || intent.getExtras() == null) {
                e.b("DummyActivity", "loginResult#cancel", true);
                o.a().onError(new ErrorStatus(3002, "getAuthTokenByFeatures : OperationCanceledException occur"));
                finish();
                return;
            }
            b(i, i2, intent);
        }
    }

    private ErrorStatus a(String str) {
        int i;
        String str2;
        String str3 = "";
        if ("AuthenticatorException".equals(str)) {
            i = 3003;
            str2 = "getAuthTokenByFeatures : AuthenticatorException occur";
            e.b("DummyActivity", "AuthenticatorException", true);
        } else if ("IOException".equals(str)) {
            i = 3004;
            str2 = "getAuthTokenByFeatures : IOException occur";
            e.b("DummyActivity", "IOException", true);
        } else if ("AccessException".equals(str)) {
            i = 15;
            str2 = "Access is not allowed";
            e.b("DummyActivity", "AccessError:appID is not allowed", true);
        } else if ("AreaNotAllowException".equals(str)) {
            i = 23;
            str2 = "AreaNotAllowError: Area is not allowed";
            e.b("DummyActivity", "AreaNotAllowError: Area is not allowed", true);
        } else if ("HwIDNotAllowException".equals(str)) {
            i = 24;
            str2 = "HwIDNotAllowError: HwID is not allowed";
            e.b("DummyActivity", "HwIDNotAllowError: HwID is not allowed", true);
        } else {
            i = 3002;
            str2 = "getAuthTokenByFeatures : OperationCanceledException occur";
            e.b("DummyActivity", "OperationCanceledException", true);
        }
        return new ErrorStatus(i, str2);
    }

    private ErrorStatus a(Bundle bundle) {
        SafeBundle safeBundle;
        String str = null;
        if (o.a() == null) {
            com.huawei.hwid.core.d.o.a((Context) this, getString(l.a(this, "CS_system_error_tip")), 1);
            e.b("DummyActivity", "callback is null, please login again!", true);
            finish();
        }
        if (bundle != null) {
            safeBundle = new SafeBundle(bundle);
            str = (String) safeBundle.get("Exception");
        } else {
            safeBundle = null;
        }
        if (str == null || !"".equals(str)) {
            return a(str);
        }
        this.d = (String) safeBundle.get("authAccount");
        this.e = (String) safeBundle.get("accountType");
        this.c = (String) safeBundle.get("authtoken");
        this.h = safeBundle.getBoolean(HwAccountConstants.EXTRA_USE_SELF_ACCOUNT, false);
        if (safeBundle.containsKey("loginUserName")) {
            this.f = safeBundle.getString("loginUserName");
        }
        if (safeBundle.containsKey("countryIsoCode")) {
            this.g = safeBundle.getString("countryIsoCode");
        }
        return new ErrorStatus(0, "");
    }

    private void a(String str, String str2, int i, SafeBundle safeBundle) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            String str3 = "authToken or accountName is null.";
            e.c("DummyActivity", str3, true);
            a(i, str3);
        } else if (this.h) {
            if (safeBundle != null) {
                Bundle bundle = safeBundle.getBundle(HwAccountConstants.EXTRA_BUNDLE);
                if (bundle != null) {
                    bundle.putBundle(HwAccountConstants.EXTRA_ENVEXTRA, safeBundle.getBundle(HwAccountConstants.EXTRA_ENVEXTRA));
                }
                this.l = bundle;
                b(this.l);
            }
            finish();
        } else {
            a(str2, true, 500);
        }
    }

    private void a(String str, boolean z, long j) {
        final Account account = new Account(str, "com.huawei.hwid");
        final Bundle bundle = new Bundle();
        bundle.putBoolean("getUserId", true);
        final String str2 = str;
        final boolean z2 = z;
        new Handler().postDelayed(new Runnable(this) {
            final /* synthetic */ DummyActivity e;

            public void run() {
                this.e.a.updateCredentials(account, this.e.b, bundle, this.e, new b(this.e, str2, z2), null);
            }
        }, j);
    }

    private void a(int i, String str) {
        Parcelable errorStatus = new ErrorStatus(i, str);
        Intent intent = new Intent();
        intent.setPackage(getPackageName());
        intent.putExtra(HwAccountConstants.EXTRA_IS_USE_SDK, false);
        intent.putExtra(HwAccountConstants.EXTRA_PARCE, errorStatus);
        c.b(this, intent);
        finish();
    }

    public void onBackPressed() {
        try {
            super.onBackPressed();
        } catch (Exception e) {
            e.d("DummyActivity", "catch Exception throw by FragmentManager!", true);
        }
    }

    private void b(Bundle bundle) {
        if (bundle == null) {
            e.b("DummyActivity", "bundle is null", true);
            return;
        }
        String str = (String) bundle.get("userId");
        int i = bundle.getInt("siteId", 0);
        String str2 = (String) bundle.get("deviceId");
        String str3 = (String) bundle.get("subDeviceId");
        String str4 = (String) bundle.get("deviceType");
        String str5 = (String) bundle.get("accountType");
        String string = bundle.getString("STValidStatus");
        String string2 = bundle.getString("uuid");
        e.b("DummyActivity", "sendSuccess", true);
        String string3 = bundle.getString(HwAccountConstants.EXTRA_AS_SERVER_DOMAIN);
        String string4 = bundle.getString(HwAccountConstants.EXTRA_CAS_SERVER_DOMAIN);
        if (com.huawei.hwid.core.d.b.e(str5) && !TextUtils.isEmpty(this.d)) {
            this.d = m.c(this.d, str5);
        }
        if (this.d != null && this.c != null) {
            Intent intent = new Intent();
            if (this.h || !com.huawei.hwid.core.d.b.c(this, "com.huawei.hwid.ICloudService")) {
                Parcelable hwAccount = new HwAccount();
                hwAccount.b(this.d);
                hwAccount.h(str2);
                hwAccount.i(str3);
                hwAccount.j(str4);
                hwAccount.a(i);
                hwAccount.f(this.c);
                hwAccount.d(str);
                hwAccount.c(this.b);
                hwAccount.g(str5);
                hwAccount.k(this.f);
                hwAccount.a(this.g);
                hwAccount.l(string);
                hwAccount.p(string2);
                hwAccount.n(string3);
                hwAccount.o(string4);
                intent.setPackage(getPackageName());
                intent.putExtra(HwAccountConstants.EXTRA_HWACCOUNT, hwAccount);
                intent.putExtra(HwAccountConstants.EXTRA_ENVEXTRA, bundle.getBundle(HwAccountConstants.EXTRA_ENVEXTRA));
            }
            intent.putExtra(HwAccountConstants.EXTRA_IS_USE_SDK, false);
            c.a(this, intent);
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        e.b("DummyActivity", "onDestroy", true);
        Bundle bundle = new Bundle();
        bundle.putBoolean("LoginBroadcastReceiver", true);
        com.huawei.hwid.api.common.e.a((Context) this, bundle);
    }

    protected void onResume() {
        super.onResume();
        e.b("DummyActivity", "onResume", true);
        if (this.i) {
            this.i = false;
            b(this.l);
            finish();
        }
    }
}
